




/*
'===============================================================================
'  HNG.Portal.BL.TblCustomerGroupManagerBase
'===============================================================================
*/

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Windows.Forms;
namespace HNG.Portal.BLL
{
	public class TblCustomerGroupManagerBase
	{
	    
		#region ColumnNames
		
				public const String Field_Id="Id";
				
				public const String Field_CusId="CusId";
				
				public const String Field_ParentId="ParentId";
				
				public const String Field_TrangThai="TrangThai";
				
		#endregion	
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public TblCustomerGroupManagerBase()
		{
			// Nothing for now.
		}
		public DataTable Clone()
		{
			DataTable dt = new DataTable();
						
			
			dt.Columns.Add(Field_Id,typeof(int));
			
						
			
			dt.Columns.Add(Field_CusId,typeof(int));
			
						
			
			dt.Columns.Add(Field_ParentId,typeof(int));
			
						
			
			dt.Columns.Add(Field_TrangThai,typeof(bool));
			
			            
            return dt;			
		}
			
		public TblCustomerGroupEntity Convert_Entity(TblCustomerGroupEntity _TblCustomerGroupEntity,TblCustomerGroupEntity _TblCustomerGroupEntity_XML)
		{	
			
				
			_TblCustomerGroupEntity.Id= _TblCustomerGroupEntity_XML.Id;
				
			_TblCustomerGroupEntity.CusId= _TblCustomerGroupEntity_XML.CusId;
				
			_TblCustomerGroupEntity.ParentId= _TblCustomerGroupEntity_XML.ParentId;
				
			_TblCustomerGroupEntity.TrangThai= _TblCustomerGroupEntity_XML.TrangThai;
				
			
			return _TblCustomerGroupEntity;
		}	
		
			
		public void InsertCollection(EntityCollection _Collection)
		{
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
            {
                adapter.SaveEntityCollection(_Collection);
            }
		}

		public TblCustomerGroupEntity Insert(TblCustomerGroupEntity TblCustomerGroupEntity)
		{
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.SaveEntity(TblCustomerGroupEntity, true);
			}
			return TblCustomerGroupEntity;
		}

		
		public TblCustomerGroupEntity Insert(int Id, int CusId, int ParentId, bool TrangThai)
		{
			TblCustomerGroupEntity _TblCustomerGroupEntity = new TblCustomerGroupEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_TblCustomerGroupEntity.Id = Id;
				_TblCustomerGroupEntity.CusId = CusId;
				_TblCustomerGroupEntity.ParentId = ParentId;
				_TblCustomerGroupEntity.TrangThai = TrangThai;
				adapter.SaveEntity(_TblCustomerGroupEntity, true);
			}
			return _TblCustomerGroupEntity;
		}

		public TblCustomerGroupEntity Insert(int CusId, int ParentId, bool TrangThai)
		{
			TblCustomerGroupEntity _TblCustomerGroupEntity = new TblCustomerGroupEntity();
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				
				_TblCustomerGroupEntity.CusId = CusId;
				_TblCustomerGroupEntity.ParentId = ParentId;
				_TblCustomerGroupEntity.TrangThai = TrangThai;
				adapter.SaveEntity(_TblCustomerGroupEntity, true);
			}
			return _TblCustomerGroupEntity;
		}

		public bool Update(TblCustomerGroupEntity _TblCustomerGroupEntity)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				RelationPredicateBucket filter = new RelationPredicateBucket();
				IPredicateExpression _PredicateExpression = new PredicateExpression();
				_PredicateExpression.Add(TblCustomerGroupFields.Id == _TblCustomerGroupEntity.Id);
					
				filter.PredicateExpression.Add(_PredicateExpression);

				adapter.UpdateEntitiesDirectly(_TblCustomerGroupEntity, filter);
				toReturn = true;
			}
			return toReturn;
		}
		
		public bool Update(TblCustomerGroupEntity _TblCustomerGroupEntity, RelationPredicateBucket filter)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.UpdateEntitiesDirectly(_TblCustomerGroupEntity, filter);
				toReturn = true;
			}
			return toReturn;
		}

		public bool Update(int Id, int CusId, int ParentId, bool TrangThai)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				TblCustomerGroupEntity _TblCustomerGroupEntity = new TblCustomerGroupEntity(Id);
				if (adapter.FetchEntity(_TblCustomerGroupEntity))
				{
				
					_TblCustomerGroupEntity.CusId = CusId;
					_TblCustomerGroupEntity.ParentId = ParentId;
					_TblCustomerGroupEntity.TrangThai = TrangThai;
					adapter.SaveEntity(_TblCustomerGroupEntity, true);
					toReturn = true;
				}
			}
			return toReturn;
		}

		public bool Delete(int Id)
		{
			bool toReturn = false;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				TblCustomerGroupEntity _TblCustomerGroupEntity = new TblCustomerGroupEntity(Id);
				if (adapter.FetchEntity(_TblCustomerGroupEntity))
				{
					adapter.DeleteEntity(_TblCustomerGroupEntity);
					toReturn = true;
				}
			}
			return toReturn;
		}
		
		public void DeleteAll() 
		{
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.DeleteEntitiesDirectly("TblCustomerGroupEntity", null);
			}
		}
		
		
		public int DeleteById(int Id)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.Id == Id);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblCustomerGroupEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByCusId(int CusId)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.CusId == CusId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblCustomerGroupEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByParentId(int ParentId)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.ParentId == ParentId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblCustomerGroupEntity", filter);
			}
			return toReturn;
		}
		
		public int DeleteByTrangThai(bool TrangThai)
		{
			int toReturn = 0;
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.TrangThai == TrangThai);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				toReturn = adapter.DeleteEntitiesDirectly("TblCustomerGroupEntity", filter);
			}
			return toReturn;
		}
		

		
		public TblCustomerGroupEntity SelectOne(int Id)
		{
			TblCustomerGroupEntity toReturn = null;
			using(DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				TblCustomerGroupEntity _TblCustomerGroupEntity = new TblCustomerGroupEntity(Id);
				if (adapter.FetchEntity(_TblCustomerGroupEntity))
				{
					toReturn = _TblCustomerGroupEntity;
				}
			}
			return toReturn;
		}

		// Return EntityCollection
		public EntityCollection SelectAll()
		{
			EntityCollection _TblCustomerGroupCollection = new EntityCollection( new TblCustomerGroupEntityFactory());
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblCustomerGroupCollection, null, 0, null);
			}
			return _TblCustomerGroupCollection;
		}
		
		// Return DataTable
		public DataTable SelectAllRDT()
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblCustomerGroupCollection.EntityFactoryToUse.CreateFields(), toReturn, null, true);
			}
			return toReturn;
		}

		
		// Return EntityCollection
		public EntityCollection SelectById(int Id)
		{
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.Id == Id);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblCustomerGroupCollection, filter, 0, null);
			}
			return _TblCustomerGroupCollection;
		}
		
		// Return DataTable
		public DataTable SelectByIdRDT(int Id)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.Id == Id);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblCustomerGroupCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectByCusId(int CusId)
		{
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.CusId == CusId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblCustomerGroupCollection, filter, 0, null);
			}
			return _TblCustomerGroupCollection;
		}
		
		// Return DataTable
		public DataTable SelectByCusIdRDT(int CusId)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.CusId == CusId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblCustomerGroupCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectByParentId(int ParentId)
		{
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.ParentId == ParentId);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblCustomerGroupCollection, filter, 0, null);
			}
			return _TblCustomerGroupCollection;
		}
		
		// Return DataTable
		public DataTable SelectByParentIdRDT(int ParentId)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.ParentId == ParentId);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblCustomerGroupCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		// Return EntityCollection
		public EntityCollection SelectByTrangThai(bool TrangThai)
		{
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();

			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.TrangThai == TrangThai);
			filter.PredicateExpression.Add(_PredicateExpression);
			
			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchEntityCollection(_TblCustomerGroupCollection, filter, 0, null);
			}
			return _TblCustomerGroupCollection;
		}
		
		// Return DataTable
		public DataTable SelectByTrangThaiRDT(bool TrangThai)
		{
			DataTable toReturn = new DataTable();
			EntityCollection _TblCustomerGroupCollection = new EntityCollection(new TblCustomerGroupEntityFactory());
			RelationPredicateBucket filter = new RelationPredicateBucket();
			
			IPredicateExpression _PredicateExpression = new PredicateExpression();
			_PredicateExpression.Add(TblCustomerGroupFields.TrangThai == TrangThai);
			filter.PredicateExpression.Add(_PredicateExpression);

			using (DataAccessAdapterBase adapter = (new DataAccessAdapterFactory()).CreateAdapter())
			{
				adapter.FetchTypedList(_TblCustomerGroupCollection.EntityFactoryToUse.CreateFields(), toReturn, filter, true);
			}
			return toReturn;
		}
		
		

	}
}
